<template>
  <div class="maintenanceStandard flowCloumn h100">
    <div class="h100">
      <list-table-new v-bind="tableSetting" ref="klTable"></list-table-new>
    </div>
    <input
      type="file"
      ref="uploadFile"
      style="display: none"
      accept=".xls,.xlsx"
      :multiple="false"
      @change="autoUploadFile"
    />
  </div>
</template>
<script>
import {
  queryExperimentList,
  setCommonExperiment,
  experimentImport,
} from "@/api/standardManagement/operateStandard.js";
export default {
  data() {
    return {
      pageType: "list",
      checked: false,
      tableSetting: {
        selectionShow: true,
        pageConfig: {
          pageCode: "experimentalStandard",
          userId: this.$store.getters.userInfo.id,
        },
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
            publicStatus: 1
          },
          searchArr: [
            {
              type: "input",
              label: "标题",
              model: "name",
              placeholder: "请输入",
            },
            {
              type: "organization",
              label: "创建单位",
              model: "deptName",
              idsStr: "deptId",
            },
            {
              type: "deviceClassify",
              label: "设备分类",
              model: "typeName",
              idsStr: "typeId",
            },
            {
              type: "select",
              label: "状态",
              model: "status",
              option: [
                {
                  value: "0",
                  label: "草稿",
                },
                {
                  value: "1",
                  label: "已发布",
                },
              ],
            },
            {
              type: "input",
              label: "编号",
              model: "code",
              placeholder: "请输入",
            },
            {
              type: "select",
              label: "适用范围",
              model: "appids",
              multiple: true,
              option: this.$store.getters.appAuthList,
            },
            {
              type: "datetimerange",
              label: "创建时间",
              model: "dates",
            },
          ],
        },
        //过滤器的值
        filterFormData: {},
        //过滤器的默认值
        defaultFilterData: {},
        userCustomData: [],
        actionList: [
          {
            name: "设为公共标准",
            icon: "icon-sheweigonggongbiaozhun11",
            disabled: !this.$store.getters.isBtnAuth("setPublic"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.toPublicStandard();
            },
          },
          {
            name: "新建",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isBtnAuth("add"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.goDetailPage(row, "experimentalStandardDetails", "add");
            },
          },
          {
            name: "导入",
            icon: "icon-daoru",
            disabled: !this.$store.getters.isBtnAuth("export"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.uploadFile();
            },
          },
          {
            name: "下载模板",
            icon: "icon-xiazai",
            disabled: !this.$store.getters.isBtnAuth("download"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.download();
            },
          },
        ],
        column: [
          {
            label: "编号",
            prop: "code",
            sortable: "custom",
          },
          {
            label: "标题",
            prop: "name",
            sortable: "custom",
          },
          {
            label: "设备分类",
            prop: "typeName",
            sortable: "custom",
          },
          {
            label: "状态",
            prop: "status",
            sortable: "custom",
            formatter: (row) => {
              return row.status == 1 ? "已发布" : "草稿";
            },
          },
          {
            label: "适用范围",
            prop: "appName",
            sortable: "custom",
          },
          {
            label: "创建单位",
            prop: "deptName",
            sortable: "custom",
          },
          {
            label: "创建人",
            prop: "createByName",
            sortable: "custom",
          },
          {
            label: "创建时间",
            prop: "createTime",
            sortable: "custom",
          },
        ],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {
            this.goDetailPage(row, "experimentalStandardDetails", "edit");
          },
        },
      },
    };
  },

  created() {
    window.addEventListener("storage", this.updateData);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted() {
    this.$refs.klTable.getTableConfig();
    this.currentAppid = this.$store.state.user.userInfo.appId;
    this.updateData();
  },
  methods: {
    updateData() {
      this.$refs.klTable.searchData();
    },
    //跳转到详情页
    goDetailPage(row, router, type) {
      const { href } = this.$router.resolve({
        name: router,
        query: {
          id: row ? row.id : "",
          // pageType: type ? type : "",
        },
      });
      window.open(href, "_blank");
    },
    // 获取表格数据
    getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      queryExperimentList(queryData).then((res) => {
        this.$set(this.tableSetting, "tableData", res.data.records);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
      });
    },
    //设置为公共标准
    toPublicStandard() {
      let arr = this.$refs.klTable.getSelectData();
      let ids = [];
      if (arr.length != 0) {
        this.$confirm("确定要设为公共标准吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            arr.forEach((item) => {
              ids.push(item.id);
            });
            setCommonExperiment({ ids: ids.join() }).then((res) => {
              if (res.code === 200) {
                this.$message.success(res.description);
                this.$refs.klTable.searchData();
              }
            });
          })
          .catch((e) => {});
      } else {
        this.$message.warning("请至少勾选一条数据");
      }
    },
    //下载模板
    download() {
      let path = "eam-service-standard/experiment/template";
      this.$utils.downloadFile(null, "试验标准导入模板", path);
    },
    //点击选择文件
    uploadFile() {
      const fileObj = this.$refs.uploadFile;
      fileObj.click();
    },
    //上传选择的文件
    async autoUploadFile(e, file) {
      let experimentFile = this.$refs.uploadFile.files[0];
      if (experimentFile.leng == 0) {
        return;
      } else {
        let params = {
          id: this.$store.state.user.userInfo.appId,
        };
        const formData = new FormData();
        formData.append("file", experimentFile);
        await experimentImport(params, formData);
        this.$refs.uploadFile.value = null;
        this.$message.success("导入成功");
        this.updateData();
      }
    },
  },
};
</script>
<style lang="less" scoped>
.maintenanceStandard {
  padding: 20px;
}
.flowCloumn {
  display: flex;
  flex-flow: column nowrap;
}
</style>